var QueryLoader = {
	overlay: "",
	loadBar: "",
	preloader: "",
	items: new Array(),
	percent: new Array(),
	doneStatus: 0,
	doneNow: 0,
	selectorPreload: "body",
	ieLoadFixTime: 2000,
	ieTimeout: "",
		
	init: function() {
		i = Math.floor((Math.random()*5));
  
		if (QueryLoader.selectorPreload == "body") {
			QueryLoader.spawnLoader();
			QueryLoader.getImages(QueryLoader.selectorPreload);
			QueryLoader.createPreloading();
		} 
		else {
			$(document).ready(function() {
				QueryLoader.spawnLoader();
				QueryLoader.getImages(QueryLoader.selectorPreload);
				QueryLoader.createPreloading();
			});
		}
		//help IE drown if it is trying to die :)
		QueryLoader.ieTimeout = setTimeout("QueryLoader.ieLoadFix()", QueryLoader.ieLoadFixTime);
	},
	ieLoadFix: function() {
		//var ie = navigator.userAgent.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/);
		//if (ie[0].match("MSIE")) {
		//	while ((100 / QueryLoader.doneStatus) * QueryLoader.doneNow < 100) {
		//		QueryLoader.imgCallback();
		//	}
		//}
	},
	imgCallback: function() {
		QueryLoader.doneNow ++;
		var perc = (100 / QueryLoader.doneStatus) * QueryLoader.doneNow;
		QueryLoader.percent.push(perc);
		if(perc > 95){
			//console.log(perc);
			QueryLoader.animateLoader();
		}
		
	},

	getImages: function(selector) {
		var everything = $(selector).find("*:not(script)").each(function() {
			var url = "";
			if ($(this).css("background-image") != "none") {
				var url = $(this).css("background-image");
			} 
			else if ($(this).attr("src") != "undefined" && $(this).prop("tagName").toLowerCase() == "img") {
				var url = $(this).attr("src");
			}						url = url.replace("url(\"","");
			url = url.replace("url(","");
			url = url.replace("\")","");
			url = url.replace(")","");
			if (url.length > 0) {
				QueryLoader.items.push(url);
			}
		});
	},
	createPreloading: function() {
		
		QueryLoader.preloader = $("<div></div>").appendTo(QueryLoader.selectorPreload);
		$(QueryLoader.preloader).css({
			height: 	"0px",
			width:		"0px",
			overflow:	"hidden"
		});
		var length = QueryLoader.items.length; 
		QueryLoader.doneStatus = length;
		
		for(var i = 0; i < length; i++){
			
			var imgLoad = $("<img></img>");
			
			$(imgLoad).attr("src", QueryLoader.items[i]);
			$(imgLoad).unbind("load");
			$(imgLoad).load(function() {
					QueryLoader.imgCallback();
			});

			$(imgLoad).appendTo($(QueryLoader.preloader));
		}
		
		
	},
	spawnLoader: function() {
		if (QueryLoader.selectorPreload == "body") {
			var height = $(window).height();
			var width = $(window).width();
			var position = "fixed";
		} else {
			var height = $(QueryLoader.selectorPreload).outerHeight();
			var width = $(QueryLoader.selectorPreload).outerWidth();
			var position = "absolute";
		}
		var left = $(QueryLoader.selectorPreload).offset()['left'];
		var top = $(QueryLoader.selectorPreload).offset()['top'];
		
		QueryLoader.overlay = $("<div></div>").appendTo($(QueryLoader.selectorPreload));
		
		$(QueryLoader.overlay).addClass("QOverlay");
		
		$('.QOverlay').css({			
			position: position,
			top: top,
			left: left,
			width: width + "px",
			height: height + "px"
		});		
		
		/* Logo Preload */		
		var strlogo = "<div><a href='http://windsorplazahotel.com' target='_blank'><img src='./images/logo_pre_wph.png' />";
		strlogo += "<a href='http://oktoberfestvietnam.com' target='_blank'><img src='./images/logo_pre_okt.png' />";		strlogo += "<a href='http://http://www.gba-vietnam.org/' target='_blank'><img src='./images/logo_pre_gpa.png' /></div>";		QueryLoader.loadBar = $(strlogo).appendTo($(QueryLoader.overlay));		$(QueryLoader.loadBar).addClass("logo_pre");				/* Random text */		var i = Math.floor((Math.random()*5));		

		QueryLoader.loadBar = $("<div></div>").appendTo($(QueryLoader.overlay));		
		$(QueryLoader.loadBar).addClass("rand_text");		
		/* End random text */				
		
		/* end logo preload */
		QueryLoader.loadBar = $("<div></div>").appendTo($(QueryLoader.overlay));
		$(QueryLoader.loadBar).addClass("QLoader");						
		$(QueryLoader.loadBar).css({
			  // position: "relative",
			  // top: "50%",
			  // width: "0%"
		});
	},
	animateLoader: function() {

		var ypos = new Array();
			ypos[0] = 25;
			ypos[1] = 313;
			ypos[2] = 619;
			ypos[3] = 915;
			ypos[4] = 1208;
			ypos[5] = 1493;
			
		
		var arrText = new Array();		
		arrText[0] = "Oktoberfest started in Munich, Germany in 1810";		
		arrText[1] = "The flag of Bavaria features blue and white diamonds";		
		arrText[2] = "Beer hostesses at Oktoberfest often wear Dirndls";		
		arrText[3] = "Oktoberfest Vietnam was first celebrated in 1992";		
		arrText[4] = "In 2012, over 15,000 people <br/> attended Oktoberfest Vietnam";
		
		var prost = "<font style='color:#61304d;font-family: prost;'>Prost!</font>";
		
		var timer = setInterval(function() {
			
			perc = QueryLoader.percent.shift();
			//console.log('__'+perc);
			if(typeof(perc) != 'undefined'){
				
				
				if (perc > 99) {
					//$(".QLoader").css({backgroundPosition: "-"+ypos[0]+"px 0px"});
					$(".QLoader").html(prost);
					QueryLoader.doneLoad();
				} else {
					if (perc < 100) {
						if (perc > 0 && perc < 10) {
							$(".rand_text").html("Did you know?");
							$(".QLoader").html('0%');
						}
							
						else if (perc > 10 && perc < 20) {
							$(".rand_text").html("Did you know?");
							$(".QLoader").html('10%');
						}
							
						else if (perc > 20 && perc < 30) {
							$(".rand_text").html(arrText[i]);
							$(".QLoader").html('20%');
						} 
						else if (perc > 30 && perc < 40) {
							$(".QLoader").html('30%');
						} 
						else if (perc > 40 && perc < 50) {
							$(".QLoader").html('40%');
						} 
						else if (perc > 50 && perc < 60) {
							$(".QLoader").html('50%');
						} 
						else if (perc > 60 && perc < 70) {
							$(".QLoader").html('60%');
						} 
						else if (perc > 70 && perc < 80) {
							$(".QLoader").html('70%');
						} 
						else if (perc > 80 && perc < 90) {
							$(".QLoader").html('80%');
						} 
						else if (perc > 90 && perc < 100) {
							$(".QLoader").html(prost);
						}
						else if (perc > 100) {
							$(".QLoader").html(prost);
						}
					} else {
						$(".QLoader").html(prost);
					}
				}
			}
			else
				clearInterval(timer);
		    
		}, 500); // 1500 ms loop
	
	},
	
	doneLoad: function() {
		
		//prevent IE from calling the fix
		clearTimeout(QueryLoader.ieTimeout);
		//determine the height of the preloader for the effect
		if (QueryLoader.selectorPreload == "body") {
			var height = $(window).height();
		}
		else {
			var height = $(QueryLoader.selectorPreload).outerHeight();
		}
		//The end animation, adjust to your likings
		$(QueryLoader.loadBar).animate({
			height: height + "px",
			top: 0
		}, 500, "linear", function() {
		
			$(QueryLoader.overlay).fadeOut(800);
			$(QueryLoader.preloader).remove();
			//$('.QOverlay').hide();
		});
	}
}